#include "MenuSystem.h"
#include "IrControl.h"
#include <fcntl.h>
#include "Menu/Tool/frame.h"


#define TEST_DISP_VER_ESC	(0)
#define TEST_DISP_VER_ESC_QTY   (1)

#define PROCPATH "/proc/version"
#define SIZE 100


TPosCoor	DispVerItemPos[TEST_DISP_VER_ESC_QTY] = {{ 577,380 }};
TRect		DispVerTouchRect[TEST_DISP_VER_ESC_QTY] ;
TRect		DispVerInfo = {0,120,720,240};
TRect		DispKeyInfo = {0,241,720,480};
int			DispVerFocus = -1;
TFrame			DispVerFrame;
char		*TitleInfoStr[2]  = {"Version:","Key:"};
char		*DispVerInfoStr[TEST_DISP_VER_ESC_QTY]  = {"ESC"};
unsigned char 	KeyData = 0;
extern unsigned char rece_handle;	
extern unsigned char dtpresent_flag;
extern pthread_cond_t dt_cond;


int DispVerGetTouchID( TPosCoor pos )
{
	int i;		
	
		for( i = 0; i < TEST_DISP_VER_ESC_QTY; i++ )
			if( ut_PtInRect( pos, DispVerTouchRect[i] ) )
				{				
				return i;
				}
		
	return -1;
}

void DispVerDrawKeyInfo( TCanvas *pCanvas)
{
	unsigned char KeyInfo[20] ;
	
	g_Puts( pCanvas, FONT33, DispKeyInfo, TEXT_ALIGN_TOP, TitleInfoStr[1] );
	sprintf( KeyInfo, "0x%x", KeyData);	
	g_Puts( pCanvas, FONT33, DispKeyInfo, TEXT_ALIGN_CENTER, "        " );
	printf ("in g_puts blank\n");
	g_Puts( pCanvas, FONT33, DispKeyInfo, TEXT_ALIGN_CENTER, KeyInfo );
	KeyData = 0;

}

void DispVerDrawItem( TCanvas *pCanvas, int idx, BOOL bfocus )
{
	TJpeg pJpeg;
	TPosCoor pos;
	char FileName[20];
	int GetJpegIdx;

	if( idx < 0 || idx >= TEST_DISP_VER_ESC_QTY )
		return;

	if( bfocus ){
		GetJpegIdx = IMG_DISP_VER_ITEM_FOCUS_ESC+idx ;
	}else{
		GetJpegIdx = IMG_DISP_VER_ITEM_ESC+idx ;
	}
	g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_WHITE, 0, 0 );
	pos = DispVerItemPos[idx];
	sprintf( FileName, "/%d/%d.jpg", TEST_VERSION_FOLDER, GetJpegIdx);	
	pJpeg = GetJpegPtr(FileName);
	DrawJpeg(pCanvas, pos.x, pos.y, FileName);
	DispVerTouchRect[idx].left = pos.x;
	DispVerTouchRect[idx].right = pos.x + pJpeg.Width;
	DispVerTouchRect[idx].top = pos.y;
	DispVerTouchRect[idx].bottom = pos.y + pJpeg.Height;
	g_Puts( pCanvas, FONT33, DispVerTouchRect[idx], TEXT_ALIGN_CENTER, DispVerInfoStr[idx] );
	g_Puts( pCanvas, FONT33, DispVerInfo, TEXT_ALIGN_TOP, TitleInfoStr[0] );
	DispVerDrawKeyInfo(pCanvas);
	if (DispVer(pCanvas))
		printf ("draw version info error!\n");
	
}


int DispVer(TCanvas *pCanvas)
{
   	FILE   	*fp   =   NULL;   
	int 		i,size = 0;
  	char*  	ver_info; 
  	char* 	p;
   
  	if   ((fp = fopen(PROCPATH,"r")) == NULL   )   
  	{   
  	printf( "open   error!\n" );   
  	return -1;
  	}  
  	 
  	ver_info = malloc (sizeof(char)*SIZE);  
  	p = ver_info;
  	printf("the  version:   %c\n", *p );  
  	while (!feof(fp)) //if is end,return 1.
  	{
  		if (fread (p,1,1,fp) != NULL)
  			{ 
  				printf("%c", *p);  
  				p ++;
  			}
  	} 
  	
  	size = strlen (ver_info);
  	printf ("size is %d\n",size);
  	for (i=0;i<size;i++)
  	{
  		if (*(ver_info+i) =='#')
  			break;
  	}
  	printf ("version is :\n");
	p = ver_info;
  	for (;i<size;i++)
  		{
  		sprintf( p, "%c", *(ver_info+i));	
		p++;
  	 	printf ("%c",*(ver_info+i));
  		}
	 p='\0';	
	g_Puts( pCanvas, FONT18, DispVerInfo, TEXT_ALIGN_CENTER, ver_info );  	 	
  	free (ver_info);
 	 fclose (fp);  
    return 0;
}




BOOL DispVerBtnEnter(int idx)
{
	if (idx <0)
		return TRUE;
	DispVerDrawItem( DispVerFrame.pCanvas, idx, FALSE );
	switch(idx)
	{
		case TEST_DISP_VER_ESC:			
			TestMainInit(1);
			dtpresent_flag++;		
			pthread_cond_signal(&dt_cond);
			break;		
	}
	LOG( "btn = %d\n", idx );	

	return TRUE;
}




void DispVerDraw( BOOL bDrawAll )
{
	int i;
	
	//g_FillRect( DispVerFrame.pCanvas, 0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, RGB_BLACK );
	DrawJpeg(DispVerFrame.pCanvas, 0, 0, "/logo.jpg" );
	for (i=0; i<TEST_DISP_VER_ESC_QTY; i++)
	DispVerDrawItem( DispVerFrame.pCanvas, i, FALSE );
		
	dtpresent_flag++;		
	pthread_cond_signal(&dt_cond);
	//dt_Present();
}





void DispVerInit(void)
{
	if (ir_GetCurrentID() ==IR_TEST_VERSION)
		{
		DispVerDrawKeyInfo(DispVerFrame.pCanvas);
		dtpresent_flag++;		
		pthread_cond_signal(&dt_cond);
		return;
		}
	ir_SetCurrentID( IR_TEST_VERSION);
	frm_Create( &DispVerFrame, FRM_NO_TITLE|FRM_NO_TASK, "" );
	DispVerDraw( TRUE );
	printf ("in DispVerInit ~~\n");
}


void DispVerInput(UInt32 key)
{
	
	if( key == IRC_TOUCH_DOWN || key == IRC_TOUCH_UP )
	{
		int focus;
		TPosCoor pos;
		dt_ClipTouchGetXY( &pos.x, &pos.y );
		if( key == IRC_TOUCH_DOWN )
		{
			if( LastKey ==IRC_TOUCH_UP )
			{
				if( ( DispVerFocus = DispVerGetTouchID( pos )) != -1 )
				{
					DispVerDrawItem( DispVerFrame.pCanvas, DispVerFocus, TRUE );
					dt_Present_part(DispVerTouchRect [DispVerFocus]);
				}
			}
		}
		else
		{
			if( ( focus = DispVerGetTouchID( pos ) ) == DispVerFocus )
				DispVerBtnEnter( DispVerFocus );
			else if( DispVerFocus != -1 )
			{
				DispVerDrawItem( DispVerFrame.pCanvas, DispVerFocus, FALSE );
				dt_Present_part(DispVerTouchRect [DispVerFocus]);
			}
			DispVerFocus = -1;
		}
		LastKey = key;//hangsheng add 071126
	}
	
}


